//
//  YunDanHuiDanShangJiaoCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/10/12.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "YunDanHuiDanShangJiaoCell.h"
#import "XLPhotoBrowser.h"

@interface YunDanHuiDanShangJiaoCell ()<XLPhotoBrowserDelegate>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *timeTitleLab;
@property (nonatomic, strong) UITextField *timeField;
@property (nonatomic, strong) UIButton *timeBtn;
@property (nonatomic, strong) UILabel *lineLab;
@property (nonatomic, strong) UILabel *fileTitleLab;
@property (nonatomic, strong) UIView *imgsView;
@property (nonatomic, strong) UIButton *sureBtn;

@end

@implementation YunDanHuiDanShangJiaoCell

+ (instancetype)YunDanHuiDanShangJiaoCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"YunDanHuiDanShangJiaoCell";
    YunDanHuiDanShangJiaoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[YunDanHuiDanShangJiaoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [UIView new];
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        self.bgView.backgroundColor = HexColor(0xffffff);
        [self.contentView addSubview:self.bgView];
        
        self.timeTitleLab = [UILabel new];
        self.timeTitleLab.textColor = HexColor(0x2B2C44);
        self.timeTitleLab.font = [UIFont systemFontOfSize:16];
        [self.bgView addSubview:self.timeTitleLab];
        
        self.timeField = [UITextField new];
        self.timeField.textColor = HexColor(0x2B2C44);;
        self.timeField.font = [UIFont systemFontOfSize:16];
        self.timeField.placeholder = @"请选择日期";
        self.timeField.userInteractionEnabled = NO;
        [self.bgView addSubview:self.timeField];
        
        self.timeBtn = [UIButton new];
        [self.timeBtn addTarget:self action:@selector(timeClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.timeBtn];
        
        self.lineLab = [UILabel new];
        self.lineLab.backgroundColor = Color_backgroundColor;
        [self.bgView addSubview:self.lineLab];
        
        self.fileTitleLab = [UILabel new];
        self.fileTitleLab.textColor = HexColor(0x2B2C44);
        self.fileTitleLab.font = [UIFont systemFontOfSize:14];
        self.fileTitleLab.text = @"附件";
        [self.bgView addSubview:self.fileTitleLab];
        
        self.imgsView = [UIView new];
        [self.bgView addSubview:self.imgsView];
        
        self.sureBtn = [UIButton new];
        self.sureBtn.layer.masksToBounds = YES;
        self.sureBtn.layer.cornerRadius = 6;
        self.sureBtn.backgroundColor = Color_ThemeColor;
        [self.sureBtn setTitle:@"确认上缴" forState:0];
        [self.sureBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.sureBtn.titleLabel.font = BoldFontOfSize(16);
        [self.sureBtn setImage:[UIImage imageNamed:@"icon_adopt"] forState:0];
        self.sureBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.sureBtn addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.sureBtn];
    }
    return self;
}

-(void)setModel:(YunDanHuiDanShangJiaoModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.bgView.nim_left = 15;
    self.bgView.nim_top = 10;
    self.bgView.nim_width = ScreenW - 30;
    
    self.timeTitleLab.text = @"上缴日期";
    self.timeTitleLab.nim_left = 15;
    self.timeTitleLab.nim_top = 19;
    [self.timeTitleLab sizeToFit];
    
    self.timeField.nim_left = self.timeTitleLab.nim_right + 25;
    self.timeField.nim_width = self.bgView.nim_width - 15 - self.timeField.nim_left;
    self.timeField.nim_height = 20;
    self.timeField.nim_centerY = self.timeTitleLab.nim_centerY;
    if (self.model.time.length) {
        self.timeField.text = self.model.time;
    }else {
        self.timeField.text = @"";
    }
    
    self.timeBtn.nim_left = self.timeTitleLab.nim_left;
    self.timeBtn.nim_top = self.timeField.nim_top;
    self.timeBtn.nim_width = ScreenW - 30;
    self.timeBtn.nim_height = self.timeField.nim_height;
    
    self.lineLab.frame = CGRectMake(15, self.timeTitleLab.nim_bottom + 19, self.bgView.nim_width - 30, 1);
    
    self.fileTitleLab.nim_left = 15;
    [self.fileTitleLab sizeToFit];
    self.fileTitleLab.nim_top = self.lineLab.nim_bottom + 20;
    
    self.imgsView.frame = CGRectMake(15, self.fileTitleLab.nim_bottom + 15, self.bgView.nim_width - 30, 90);
    CGFloat imgW = (self.imgsView.nim_width - 40) * 0.33333;
    self.imgsView.nim_height = imgW + 10;
    __block CGFloat viewLeft = 0;
    [self.imgsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.model.imgList enumerateObjectsUsingBlock:^(YunDanHuiDanShangJiaoImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton new];
        btn.nim_left = (imgW + 20) * idx;
        btn.nim_top = 10;
        btn.nim_width = imgW;
        btn.nim_height = imgW;
        [btn sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",obj.url,obj.filepath]] forState:0];
        btn.tag = idx;
        [btn addTarget:self action:@selector(imgPreView:) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:btn];
        
        UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(btn.nim_right - 15, btn.nim_top - 7, 22, 22)];
        [deleteBtn setImage:[UIImage imageNamed:@"icon_caozuodan_delete_img"] forState:0];
        deleteBtn.tag = idx;
        [deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:deleteBtn];
        
        viewLeft = btn.nim_right + 20;
    }];
    
    if (self.model.imgList.count < 3) {
        UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(viewLeft, 10, imgW, imgW)];
        [addBtn setBackgroundImage:[UIImage imageNamed:@"icon_caozuodan_upload_img"] forState:0];
        [addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
        [self.imgsView addSubview:addBtn];
    }
    
    self.bgView.nim_height = self.imgsView.nim_bottom + 15;
    
    self.sureBtn.nim_left = self.bgView.nim_left;
    self.sureBtn.nim_top = self.bgView.nim_bottom + 20;
    self.sureBtn.nim_width = self.bgView.nim_width;
    self.sureBtn.nim_height = 44;
    
    self.height = self.sureBtn.nim_bottom + 10;
}

- (void)imgPreView:(UIButton *)sender {
    NSMutableArray *mut = [NSMutableArray array];
    [self.model.imgList enumerateObjectsUsingBlock:^(YunDanHuiDanShangJiaoImgModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mut addObject:[NSString stringWithFormat:@"%@%@",obj.url,obj.filepath]];
    }];
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:mut currentImageIndex:sender.tag];
    browser.browserStyle = XLPhotoBrowserStyleIndexLabel;//微博样式
    [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:nil deleteButtonTitle:nil otherButtonTitles:@"保存图片", nil];
}

- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex {
    [browser saveCurrentShowImage];
}

- (void)deleteClick:(UIButton *)sender {
    if (self.deleteBlock) {
        self.deleteBlock(sender.tag);
    }
}

- (void)addClick {
    if (self.addBlock) {
        self.addBlock();
    }
}

- (void)sureClick {
    if (self.sureBlock) {
        self.sureBlock();
    }
}

- (void)timeClick {
    if (self.timeBlock) {
        self.timeBlock();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
